<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14/014
 * Time: 14:14
 *
 * Description: 获取订单列表 pc端用户中心
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Order;
use App\Product;
use App\Product_type;
use DB;

class RechargeController extends Controller
{

    public function getOrderList(Request $request)
    {

        $page_size = 10;
        $page_no = 0;
        //根据用户登录token获取user_id;
        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => "非法参数",
                'data' => array()
            );
            return response()->json($return);
        }else{
            $token = $request->input('token');
            $user_id = UsersController::getIdByWebToken($token);

            if(!$user_id)
            {
                $return = array(
                    'code' => -1,
                    'msg' => "非法参数",
                    'data' => array()
                );
                return response()->json($return);
            }

        }
        if($request->has('page_no'))
        {
            $page_no = $request->input('page_no')-1;
            if( $page_no < 0) {
                $page_no = 0;
            }
        }

        if($request->has('page_size'))
        {
            $page_size = $request->input('page_size');
            if( $page_size <= 0) {
                $page_size = 10;
            }
        }
        $where = ['user_id'=>$user_id,'status'=>1];
        //获取订单信息列表
        $Orders = Order::where($where)->orderBy('created_at', 'desc')->offset($page_no*$page_size)->limit($page_size)->get(['order_no','date','price','product_id','created_at'])->toArray();

        $OrderList = [];
        foreach($Orders as $key=>$value)
        {
            $product = Product::where('id',6)->get(['name','type_id']);

            $product_type = Product_type::where('type_id',$product[0]['type_id'])->get(['type_name']);

            $value['product_type'] = $product_type[0]['type_name'] . $product[0]['name'];

            $OrderList[$key]['order_no'] = $value['order_no'];
            $OrderList[$key]['price'] = $value['price'];
            $OrderList[$key]['product_type'] = $value['product_type'];
            $OrderList[$key]['created_at'] = $value['created_at'];
            $OrderList[$key]['end_at'] = date("Y-m-d H:i:s",strtotime("+". $value['date'] ." day",strtotime($value['created_at'])));

        }

        $count = $count = Order::where($where)->count();;

        return self::ShowData($OrderList,$count);
    }



}